//模块化
//seckill.detail.init(params)
var seckill = {
    URL : {
        NOW:"/seckill/time/now",
        exposer:function(seckillId){
            return '/seckill/'+seckillId+'/exposer';
        },
        execution:function(seckillId,md5){
            return '/seckill/'+seckillId+'/'+md5+"/execution";
        }
    },
    //验证手机号
    validatePhone: function (phone) {
        if(phone && phone.length==11 && !isNaN(phone)){
            return true;
        }else{
            return false;
        }
    },
    handleSeckill:function(seckillId,node){
      //处理秒杀逻辑
        node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');
        $.post(seckill.URL.exposer(seckillId),{},function(result){
            if(result && result['success']){
                var exposer = result['data'];
                if(exposer['exposed']){
                    //开启秒杀，获取秒杀地址
                    var md5 = exposer['md5'];
                    var killUrl = seckill.URL.execution(seckillId,md5);
                    console.log('killUrl:'+killUrl);
                    //绑定一次点击事件，防止重复提交
                    $('#killBtn').one('click',function(){
                        //执行秒杀请求
                        //1.先禁用按钮
                        $(this).addClass('disabled');
                        //2.发送秒杀请求
                        $.post(killUrl,{},function(result){
                            if(result && result['success']){
                                var killResult = result['data'];
                                var state = killResult['status'];
                                var stateInfo = killResult['statusInfo'];
                                //3.显示秒杀结果
                                node.html('<span class="label label-success">'+stateInfo+'</span>');
                            }
                        });
                    });
                    node.show();
                }else{
                    var now = exposer['now'];
                    var start = exposer['start'];
                    var end = exposer['end'];
                    seckill.countdown(seckillId,now,start,end);
                }
            }
        });
    },
    countdown:function(seckillId,nowTime,startTime,endTime){
        var seckillBox = $('#seckill-box');
        if(nowTime > endTime){
            seckillBox.html('秒杀结束!');
        }else if(nowTime < startTime){
            //秒杀未开始，倒计时
            var killTime = new Date(startTime + 1000);
            seckillBox.countdown(killTime,function(event){
                var format = event.strftime('秒杀倒数计时：%D天 %H小时 %M分 %S秒');
                seckillBox.html(format);
            }).on('finish.countdown',function(){
                //显示秒杀按钮
                seckill.handleSeckill(seckillId,seckillBox);
            });
        }else{
            seckill.handleSeckill(seckillId,seckillBox);
        }
    },
    detail:{
        init:function(params){
            //手机验证和登录，计时交互
            var killPhone = $.cookie('userPhone');
            var startTime = params['startTime'];
            var endTime = params['endTime'];
            var seckillId = params['seckillId'];
            if(!seckill.validatePhone(killPhone)){
                var killPhoneModal = $("#killPhoneModal");
                killPhoneModal.modal({
                    show:true,
                    backdrop:"static",
                    keyboard:false
                });
                $('#killPhoneBtn').click(function(){
                    var inputPhone = $('#killPhoneKey').val();
                    //console.log("inputPhone="+inputPhone);//todo
                    if(seckill.validatePhone(inputPhone)){
                        //电话写入cookie
                        $.cookie('userPhone',inputPhone,{expires:7,path:'/seckill'});
                        window.location.reload();
                    }else{
                        $("#killPhoneMessage").hide().html('<label class="label label-danger">手机号错误！</label>').show(500);
                    }
                });
            }
            //已经登陆，计时交互
            $.get(seckill.URL.NOW,{},function(result){
                if(result && result['data']){
                    var nowTime = result['data'];
                    seckill.countdown(seckillId,nowTime,startTime,endTime);
                }else{
                    console.log('result:'+result);
                }
            });
        }
    }
}